package com.yunchang.springboot.service.transactionMethod;

import com.yunchang.springboot.mapper.base.FinanceShopSkuMapper;
import com.yunchang.springboot.mapper.caravan.CaravanBillsDataMapper;
import com.yunchang.springboot.mapper.caravan.CaravanOrderDataMapper;
import com.yunchang.springboot.mapper.caravan.CaravanPurchaseDataMapper;
import com.yunchang.springboot.mapper.caravan.CaravanStorageLogDataMapper;
import com.yunchang.springboot.mapper.finance.*;
import com.yunchang.springboot.mapper.logisticsReconciliation.WlCreateAPlanMapper;
import com.yunchang.springboot.mapper.logisticsReconciliation.WlFreightAllocationMapper;
import com.yunchang.springboot.models.DO.CaravanOrderData;
import com.yunchang.springboot.models.DO.CaravanPurchaseDataItem;
import com.yunchang.springboot.models.DO.CaravanStorageLogData;
import com.yunchang.springboot.models.DO.finance.*;
import com.yunchang.springboot.models.DO.logisticsReconciliation.WlCreateAPlan;
import com.yunchang.springboot.models.base.FinanceShopSku;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.interceptor.TransactionAspectSupport;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.List;

@Service
@Slf4j
public class FinanceDebitCreditService {
    @Resource
    CaravanStorageLogDataMapper storageLogDataMapper;
    @Resource
    CaravanPurchaseDataMapper purchaseDataMapper;
    @Resource
    CaravanBillsDataMapper billsDataMapper;
    @Resource
    CaravanOrderDataMapper orderDataMapper;
    @Resource
    FinanceCaravanDebitCreditRelationMapper financeCaravanDebitCreditRelationMapper;
    @Resource
    FinanceShopSkuMapper shopSkuMapper;
    @Resource
    FinanceSubjectMapper subjectMapper;
    //    @Resource
//    FinanceCaravanProcessingOrderMapper caravanProcessingOrderMapper;
    @Resource
    FinanceCaravanProcessingOrderItemMapper caravanProcessingOrderItemMapper;
    @Resource
    FinanceCaravanDocumentTypeMapper financeCaravanDocumentTypeMapper;
    @Resource
    FinanceCaravanShopNameRelationMapper financeCaravanShopNameRelationMapper;
    @Resource
    FinanceOpeningInventoryMapper financeOpeningInventoryMapper;
    @Resource
    WlFreightAllocationMapper wlFreightAllocationMapper;
    @Resource
    WlCreateAPlanMapper wlCreateAPlanMapper;

    /**
     * 马帮出入库数据生成借贷关系的事务方法
     */
    @Transactional(rollbackFor = Exception.class)
    public int calculateStorageLogDataWithTransaction(CaravanStorageLogData storageLogData, int flag) {
        //创建事务
        Object savepoint = TransactionAspectSupport.currentTransactionStatus().createSavepoint();
        try {
            //创建存放借贷关系数据的集合
            List<FinanceCaravanDebitCreditRelation> financeCaravanDebitCreditRelationList = new ArrayList<>();
            String logId = storageLogData.getLogId();
            int handleStatus = storageLogData.getHandleStatus();
            //如果是新增的数据
            if (handleStatus == 0) {
                //根据具体的单据类型，生成对应借贷关系
                //这边只是生成科目
                log.info("开始生成借贷关系");
                financeCaravanDebitCreditRelationList = generateStorageLogDataDebitCreditSubject(storageLogData);
                storageLogData.setHandleStatus(1);
            } else {
                log.info("开始获取原有的借贷关系");
                financeCaravanDebitCreditRelationList = financeCaravanDebitCreditRelationMapper.getDataByRecordIdAndRecordType(logId, 5);
            }
            //判断生成的借贷关系中是否有手动处理的科目
            ISMANUAL:
            for (FinanceCaravanDebitCreditRelation debitCreditRelation : financeCaravanDebitCreditRelationList) {
                if (debitCreditRelation.getSubjectId() == 1000) {
                    flag = 0;
                    break ISMANUAL;
                }
            }
            //如果flag是1，表示当前已经没有手动处理的科目，可以开始计算具体的数值
            if (flag == 1) {
                //先计算每条数据的库存单价和剩余库存,然后根据每条数据的具体单据类型计算借贷关系的数值
                log.info("开始生成借贷关系金额");
                generateStorageLogDataDebitCreditRelationValue(storageLogData, financeCaravanDebitCreditRelationList);
            }
            //更新出入库流水数据的状态和剩余库存、库存单价
            log.info("更新出入库数据的状态和剩余库存、库存单价");
            storageLogDataMapper.updateHandleStatusAndAmount(storageLogData);
            log.info("更新出入库数据的状态和剩余库存、库存单价成功");
            //将生成的借贷关系存入数据库中
            if (handleStatus == 0) {
                log.info("新增财务数据");
                financeCaravanDebitCreditRelationMapper.insertList(financeCaravanDebitCreditRelationList);
                log.info("新增财务数据成功");
            } else {
                log.info("更新财务数据");
                financeCaravanDebitCreditRelationMapper.replaceDataList(financeCaravanDebitCreditRelationList);
                log.info("更新财务数据成功");
            }
        } catch (Exception e) {
            //回滚事务
            TransactionAspectSupport.currentTransactionStatus().rollbackToSavepoint(savepoint);
            e.printStackTrace();
        }
        return flag;
    }

    /**
     * 根据具体单据类型生成借贷关系的科目名称
     */
    private List<FinanceCaravanDebitCreditRelation> generateStorageLogDataDebitCreditSubject(CaravanStorageLogData storageLogData) {
        List<FinanceCaravanDebitCreditRelation> financeCaravanDebitCreditRelationList = new ArrayList<>();
        String documentNum = storageLogData.getDocumentNum();
        String sku = storageLogData.getStockSku();
        String documentName = storageLogData.getDocumentType();
        String warehouse = storageLogData.getWearhouseName();
        String timeCreate = storageLogData.getTimeCreate();
        if ("手工出库".equals(documentName) || "手工入库".equals(documentName)) {
            int quantity = storageLogData.getQuantity();
            if (quantity >= 0) {
                documentName = "手工入库";
            } else {
                documentName = "手工出库";
            }
        }
        FinanceCaravanDocumentType financeCaravanDocumentType = financeCaravanDocumentTypeMapper.getInfoByRecordTypeAndDocumentName(5, documentName);
        int documentType = financeCaravanDocumentType.getDocumentType();
        switch (documentType) {
            case 14:  //"采购入库"
                //把quantity取绝对值
                //这里需要去查到关联的采购单数据，看看是不是实际入库的比采购单的多，如果是，则再加一组借贷关系
                CaravanPurchaseDataItem caravanPurchaseDataItem = purchaseDataMapper.getPurchaseDataItemByPurchaseIdAndSku(documentNum, sku);
                FinanceCaravanDebitCreditRelation debitCreditRelation1 = loadStorageLogDataDebitCreditRelation(storageLogData, documentType, 1406, 1);
                FinanceCaravanDebitCreditRelation debitCreditRelation2 = loadStorageLogDataDebitCreditRelation(storageLogData, documentType, 140206, 2);
                FinanceCaravanDebitCreditRelation debitCreditRelation3 = loadStorageLogDataDebitCreditRelation(storageLogData, documentType, 220211, 1);
                //获得之前所有的关于这个采购单的出入库流水的库存数量总和
                Integer quantitySum = storageLogDataMapper.getQuantitySumByDocumentTypeSkuWarehouseDocumentNumTime(documentName, sku, warehouse, documentNum, timeCreate);
                if (caravanPurchaseDataItem != null && quantitySum > caravanPurchaseDataItem.getPurchaseNum()) {
                    FinanceCaravanDebitCreditRelation debitCreditRelation6 = loadStorageLogDataDebitCreditRelation(storageLogData, documentType, 140206, 1);
                    //将计算后的正确sku数量放入
                    debitCreditRelation6.setSkuNum(quantitySum - caravanPurchaseDataItem.getPurchaseNum());
                    FinanceCaravanDebitCreditRelation debitCreditRelation7 = loadStorageLogDataDebitCreditRelation(storageLogData, documentType, 220211, 2);
                    //同上
                    debitCreditRelation7.setSkuNum(quantitySum - caravanPurchaseDataItem.getPurchaseNum());
                    financeCaravanDebitCreditRelationList.add(debitCreditRelation6);
                    financeCaravanDebitCreditRelationList.add(debitCreditRelation7);
                }
                FinanceCaravanDebitCreditRelation debitCreditRelation4 = loadStorageLogDataDebitCreditRelation(storageLogData, documentType, 220212, 2);
                FinanceCaravanDebitCreditRelation debitCreditRelation5 = loadStorageLogDataDebitCreditRelation(storageLogData, documentType, 112301, 2);
                financeCaravanDebitCreditRelationList.add(debitCreditRelation1);
                financeCaravanDebitCreditRelationList.add(debitCreditRelation2);
                financeCaravanDebitCreditRelationList.add(debitCreditRelation3);
                financeCaravanDebitCreditRelationList.add(debitCreditRelation4);
                financeCaravanDebitCreditRelationList.add(debitCreditRelation5);
                log.info("采购入库借贷关系生成成功");
                break;
            case 15://采购单退货出库
                debitCreditRelation1 = loadStorageLogDataDebitCreditRelation(storageLogData, documentType, 140206, 1);
                debitCreditRelation2 = loadStorageLogDataDebitCreditRelation(storageLogData, documentType, 1406, 2);
                debitCreditRelation3 = loadStorageLogDataDebitCreditRelation(storageLogData, documentType, 220211, 1);
                debitCreditRelation4 = loadStorageLogDataDebitCreditRelation(storageLogData, documentType, 220212, 2);
                debitCreditRelation5 = loadStorageLogDataDebitCreditRelation(storageLogData, documentType, 112301, 2);
                financeCaravanDebitCreditRelationList.add(debitCreditRelation1);
                financeCaravanDebitCreditRelationList.add(debitCreditRelation2);
                financeCaravanDebitCreditRelationList.add(debitCreditRelation3);
                financeCaravanDebitCreditRelationList.add(debitCreditRelation4);
                financeCaravanDebitCreditRelationList.add(debitCreditRelation5);
                log.info("采购单退货出库借贷关系生成成功");
                break;
            case 6://加工原材料出库
                debitCreditRelation1 = loadStorageLogDataDebitCreditRelation(storageLogData, documentType, 1403, 1);
                //将负数的数量改为正数
                debitCreditRelation1.setSkuNum(Math.abs(storageLogData.getQuantity()));
                debitCreditRelation2 = loadStorageLogDataDebitCreditRelation(storageLogData, documentType, 1406, 2);
                //将负数的数量改为正数
                debitCreditRelation2.setSkuNum(Math.abs(storageLogData.getQuantity()));
                financeCaravanDebitCreditRelationList.add(debitCreditRelation1);
                financeCaravanDebitCreditRelationList.add(debitCreditRelation2);
                log.info("加工原材料出库借贷关系生成成功");
                break;
            case 7://加工商品入库
                debitCreditRelation1 = loadStorageLogDataDebitCreditRelation(storageLogData, documentType, 1406, 1);
                debitCreditRelation2 = loadStorageLogDataDebitCreditRelation(storageLogData, documentType, 1403, 2);
                financeCaravanDebitCreditRelationList.add(debitCreditRelation1);
                financeCaravanDebitCreditRelationList.add(debitCreditRelation2);
                log.info("加工商品入库借贷关系生成成功");
                break;
            case 4://分仓调拨入库
                debitCreditRelation1 = loadStorageLogDataDebitCreditRelation(storageLogData, documentType, 1406, 1);
                debitCreditRelation2 = loadStorageLogDataDebitCreditRelation(storageLogData, documentType, 140202, 2);
                financeCaravanDebitCreditRelationList.add(debitCreditRelation1);
                financeCaravanDebitCreditRelationList.add(debitCreditRelation2);
                log.info("分仓调拨入库借贷关系生成成功");
                break;
            case 5://分仓调拨出库
                debitCreditRelation1 = loadStorageLogDataDebitCreditRelation(storageLogData, documentType, 140202, 1);
                //将负数的数量改为正数
                debitCreditRelation1.setSkuNum(Math.abs(storageLogData.getQuantity()));
                debitCreditRelation2 = loadStorageLogDataDebitCreditRelation(storageLogData, documentType, 1406, 2);
                //将负数的数量改为正数
                debitCreditRelation2.setSkuNum(Math.abs(storageLogData.getQuantity()));
                financeCaravanDebitCreditRelationList.add(debitCreditRelation1);
                financeCaravanDebitCreditRelationList.add(debitCreditRelation2);
                log.info("分仓调拨出库借贷关系生成成功");
                break;
            case 3://分仓调拨作废入库
                debitCreditRelation1 = loadStorageLogDataDebitCreditRelation(storageLogData, documentType, 1406, 1);
                debitCreditRelation2 = loadStorageLogDataDebitCreditRelation(storageLogData, documentType, 140202, 2);
                financeCaravanDebitCreditRelationList.add(debitCreditRelation1);
                financeCaravanDebitCreditRelationList.add(debitCreditRelation2);
                log.info("分仓调拨作废入库借贷关系生成成功");
                break;
            case 13://退货单验货入库
                //先根据sku、关联的订单号和时间去借贷关系表中查是否有对应的借贷关系数据，有，就直接根据这个借贷关系生成科目就行；
                // 没有就生成 借：库存商品（金额为仓库近期的单价*数量）   贷：主营业务成本-历史退货
                List<FinanceCaravanDebitCreditRelation> orderIssueDebitCreditRelationList = financeCaravanDebitCreditRelationMapper.getDataByDocumentTypeSkuWarehouseRelatedRecordNoTime(documentType, sku, warehouse, documentNum, timeCreate);
                if (orderIssueDebitCreditRelationList == null && orderIssueDebitCreditRelationList.size() == 0) {
                    debitCreditRelation1 = loadStorageLogDataDebitCreditRelation(storageLogData, documentType, 1406, 1);
                    debitCreditRelation2 = loadStorageLogDataDebitCreditRelation(storageLogData, documentType, 640106, 2);
                    financeCaravanDebitCreditRelationList.add(debitCreditRelation1);
                    financeCaravanDebitCreditRelationList.add(debitCreditRelation2);
                } else {
                    //这里的科目金额就在这边直接计算完了
                    for (int i = 0; i < orderIssueDebitCreditRelationList.size(); i++) {
                        FinanceCaravanDebitCreditRelation debitCreditRelation = orderIssueDebitCreditRelationList.get(i);
                        Integer subjectId = debitCreditRelation.getSubjectId();
                        Integer quantity = storageLogData.getQuantity();
                        if (subjectId == 640105) {
                            debitCreditRelation.setSkuNum(quantity);
                            debitCreditRelation.setAmount(debitCreditRelation.getAmount().divide(BigDecimal.valueOf(debitCreditRelation.getSkuNum()), 4, BigDecimal.ROUND_HALF_UP).multiply(BigDecimal.valueOf(quantity)));
                            financeCaravanDebitCreditRelationList.add(debitCreditRelation);
                        }
                        if (subjectId == 1406) {
                            debitCreditRelation.setSkuNum(quantity);
                            debitCreditRelation.setAmount(debitCreditRelation.getAmount().divide(BigDecimal.valueOf(debitCreditRelation.getSkuNum()), 4, BigDecimal.ROUND_HALF_UP).multiply(BigDecimal.valueOf(quantity)));
                            financeCaravanDebitCreditRelationList.add(debitCreditRelation);
                        }
                        if (subjectId == 630110) {
                            debitCreditRelation.setSkuNum(quantity);
                            debitCreditRelation.setAmount(debitCreditRelation.getAmount().divide(BigDecimal.valueOf(debitCreditRelation.getSkuNum()), 4, BigDecimal.ROUND_HALF_UP).multiply(BigDecimal.valueOf(quantity)));
                            financeCaravanDebitCreditRelationList.add(debitCreditRelation);
                        }
                    }
                }
                log.info("退货单验货入库借贷关系生成成功");
                break;
            case 10://手工出库
                debitCreditRelation1 = loadStorageLogDataDebitCreditRelation(storageLogData, documentType, 1000, 1);
                //将负数的数量改为正数
                debitCreditRelation1.setSkuNum(Math.abs(storageLogData.getQuantity()));
                debitCreditRelation2 = loadStorageLogDataDebitCreditRelation(storageLogData, documentType, 1406, 2);
                //将负数的数量改为正数
                debitCreditRelation2.setSkuNum(Math.abs(storageLogData.getQuantity()));
                financeCaravanDebitCreditRelationList.add(debitCreditRelation1);
                financeCaravanDebitCreditRelationList.add(debitCreditRelation2);
                log.info("手工出库借贷关系生成成功");
                break;
            case 9://手工入库
                debitCreditRelation1 = loadStorageLogDataDebitCreditRelation(storageLogData, documentType, 1406, 1);
                debitCreditRelation2 = loadStorageLogDataDebitCreditRelation(storageLogData, documentType, 1000, 2);
                financeCaravanDebitCreditRelationList.add(debitCreditRelation1);
                financeCaravanDebitCreditRelationList.add(debitCreditRelation2);
                log.info("手工入库借贷关系生成成功");
                break;
            case 2://修改库存
                int quantity = storageLogData.getQuantity();
                if (quantity >= 0) {
                    debitCreditRelation1 = loadStorageLogDataDebitCreditRelation(storageLogData, documentType, 1406, 1);
                    debitCreditRelation2 = loadStorageLogDataDebitCreditRelation(storageLogData, documentType, 630108, 2);
                    financeCaravanDebitCreditRelationList.add(debitCreditRelation1);
                    financeCaravanDebitCreditRelationList.add(debitCreditRelation2);
                } else {
                    debitCreditRelation1 = loadStorageLogDataDebitCreditRelation(storageLogData, documentType, 1406, 1);
                    //将负数的数量改为正数
                    debitCreditRelation1.setSkuNum(Math.abs(storageLogData.getQuantity()));
                    debitCreditRelation2 = loadStorageLogDataDebitCreditRelation(storageLogData, documentType, 671104, 2);
                    //将负数的数量改为正数
                    debitCreditRelation2.setSkuNum(Math.abs(storageLogData.getQuantity()));
                    financeCaravanDebitCreditRelationList.add(debitCreditRelation1);
                    financeCaravanDebitCreditRelationList.add(debitCreditRelation2);
                }
                log.info("修改库存借贷关系生成成功");
                break;
            case 12://订单出库
                //这里需要去查到关联的订单数据，看订单中到达的店铺名是否和这个sku匹配的店铺名相同
                CaravanOrderData caravanOrderData = orderDataMapper.getShopNameByPlatformOrderId(documentNum);
                String targetShopName = caravanOrderData.getShopName();
                String platformId = caravanOrderData.getPlatformId();
                FinanceShopSku financeShopSku = shopSkuMapper.getInfoBySku(sku);
                String sellerId = financeShopSku.getSellerId();
                String area = financeShopSku.getArea();
                FinanceCaravanShopNameRelation financeCaravanShopNameRelation = financeCaravanShopNameRelationMapper.getInfoByCaravanShopNameAndPlatform(targetShopName, platformId);
                String targetSellerId = "";
                String targetArea = "";
                String shopName = "";
                if (financeCaravanShopNameRelation != null) {
                    targetSellerId = financeCaravanShopNameRelation.getSelllerId();
                    targetArea = financeCaravanShopNameRelation.getArea();
                    shopName = financeCaravanShopNameRelation.getShopName();
                }
                if (sellerId.equals(targetSellerId) && area.equals(targetArea)) {
                    debitCreditRelation1 = loadStorageLogDataDebitCreditRelation(storageLogData, documentType, 640105, 1);
                    //将负数的数量改为正数
                    debitCreditRelation1.setSkuNum(Math.abs(storageLogData.getQuantity()));
                    debitCreditRelation2 = loadStorageLogDataDebitCreditRelation(storageLogData, documentType, 1406, 2);
                    //将负数的数量改为正数
                    debitCreditRelation2.setSkuNum(Math.abs(storageLogData.getQuantity()));
                    financeCaravanDebitCreditRelationList.add(debitCreditRelation1);
                    financeCaravanDebitCreditRelationList.add(debitCreditRelation2);
                } else {
                    //这个借贷关系的店铺信息需要变成目标店铺（targetShopName）的信息
                    debitCreditRelation1 = loadStorageLogDataDebitCreditRelation(storageLogData, documentType, 640105, 1);
                    debitCreditRelation1.setSellerId(targetSellerId);
                    debitCreditRelation1.setArea(targetArea);
                    debitCreditRelation1.setShopName(shopName);
                    //将负数的数量改为正数
                    debitCreditRelation1.setSkuNum(Math.abs(storageLogData.getQuantity()));
                    debitCreditRelation2 = loadStorageLogDataDebitCreditRelation(storageLogData, documentType, 1406, 2);
                    //将负数的数量改为正数
                    debitCreditRelation2.setSkuNum(Math.abs(storageLogData.getQuantity()));
                    debitCreditRelation3 = loadStorageLogDataDebitCreditRelation(storageLogData, documentType, 630110, 2);
                    //将负数的数量改为正数
                    debitCreditRelation3.setSkuNum(Math.abs(storageLogData.getQuantity()));
                    financeCaravanDebitCreditRelationList.add(debitCreditRelation1);
                    financeCaravanDebitCreditRelationList.add(debitCreditRelation2);
                    financeCaravanDebitCreditRelationList.add(debitCreditRelation3);
                }
                log.info("订单出库借贷关系生成成功");
                break;
            case 1://FBA发货出库
                //这里需要去查到关联的亚马逊发货批次数据，看到达的店铺名是否和这个sku匹配的店铺名相同
                //从创建计划表wl_create_a_plan获取shipmentId对应的店铺信息
                WlCreateAPlan wlCreateAPlan = wlCreateAPlanMapper.getShopDataByShipmentId(documentNum);
                financeShopSku = shopSkuMapper.getInfoBySku(sku);
                sellerId = financeShopSku.getSellerId();
                area = financeShopSku.getArea();
                targetSellerId = "";
                targetArea = "";
                targetShopName = "";
                String positions = "";
                if (wlCreateAPlan != null) {
                    targetSellerId = wlCreateAPlan.getSellerId();
                    targetArea = wlCreateAPlan.getCountryName();
                    targetShopName = wlCreateAPlan.getShopName();
                    positions = wlCreateAPlan.getPositions();
                }
                //这边的subjectId需要去查找一下目的地是哪个仓
                Integer debitSubjectId = null;
                if ("美西海外仓".equals(positions)) {
                    debitSubjectId = 140207;
                } else if ("美东海外仓".equals(positions)) {
                    debitSubjectId = 140208;
                } else {
                    debitSubjectId = 140209;
                }
                if (sellerId.equals(targetSellerId) && area.equals(targetArea)) {
                    debitCreditRelation1 = loadStorageLogDataDebitCreditRelation(storageLogData, documentType, debitSubjectId, 1);
                    //将负数的数量改为正数
                    debitCreditRelation1.setSkuNum(Math.abs(storageLogData.getQuantity()));
                    debitCreditRelation2 = loadStorageLogDataDebitCreditRelation(storageLogData, documentType, 1406, 2);
                    //将负数的数量改为正数
                    debitCreditRelation2.setSkuNum(Math.abs(storageLogData.getQuantity()));
                    financeCaravanDebitCreditRelationList.add(debitCreditRelation1);
                    financeCaravanDebitCreditRelationList.add(debitCreditRelation2);
                } else {
                    //这个借贷关系的店铺信息需要变成目标店铺（targetShopName）的信息
                    debitCreditRelation1 = loadStorageLogDataDebitCreditRelation(storageLogData, documentType, debitSubjectId, 1);
                    debitCreditRelation1.setSellerId(targetSellerId);
                    debitCreditRelation1.setShopName(targetShopName);
                    debitCreditRelation1.setArea(targetArea);
                    //将负数的数量改为正数
                    debitCreditRelation1.setSkuNum(Math.abs(storageLogData.getQuantity()));
                    debitCreditRelation2 = loadStorageLogDataDebitCreditRelation(storageLogData, documentType, 1406, 2);
                    //将负数的数量改为正数
                    debitCreditRelation2.setSkuNum(Math.abs(storageLogData.getQuantity()));
                    debitCreditRelation3 = loadStorageLogDataDebitCreditRelation(storageLogData, documentType, 630110, 2);
                    //将负数的数量改为正数
                    debitCreditRelation3.setSkuNum(Math.abs(storageLogData.getQuantity()));
                    financeCaravanDebitCreditRelationList.add(debitCreditRelation1);
                    financeCaravanDebitCreditRelationList.add(debitCreditRelation2);
                    financeCaravanDebitCreditRelationList.add(debitCreditRelation3);
                }
                log.info("FBA发货出库借贷关系生成成功");
                break;
            case 8://库存盘点
            case 11://海外仓出入库
            case 16://excel导入
                debitCreditRelation1 = loadStorageLogDataDebitCreditRelation(storageLogData, documentType, 1000, 1);
                //将负数的数量改为正数
                debitCreditRelation1.setSkuNum(Math.abs(storageLogData.getQuantity()));
                debitCreditRelation2 = loadStorageLogDataDebitCreditRelation(storageLogData, documentType, 1000, 2);
                //将负数的数量改为正数
                debitCreditRelation2.setSkuNum(Math.abs(storageLogData.getQuantity()));
                financeCaravanDebitCreditRelationList.add(debitCreditRelation1);
                financeCaravanDebitCreditRelationList.add(debitCreditRelation2);
                log.info("全手动处理借贷关系生成成功");
                break;
            default:
                storageLogData.setHandleStatus(1);
                break;
        }
        return financeCaravanDebitCreditRelationList;
    }

    /**
     * 根据具体单据类型生成借贷关系的值
     */
    private void generateStorageLogDataDebitCreditRelationValue(CaravanStorageLogData storageLogData, List<FinanceCaravanDebitCreditRelation> financeCaravanDebitCreditRelationList) {
        DateTimeFormatter df = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
        String documentNum = storageLogData.getDocumentNum();
        String sku = storageLogData.getStockSku();
        Integer quantity = storageLogData.getQuantity();
        String warehouse = storageLogData.getWearhouseName();
        String timeCreate = storageLogData.getTimeCreate();
        String documentName = storageLogData.getDocumentType();
        if ("手工出库".equals(documentName) || "手工入库".equals(documentName)) {
            if (quantity >= 0) {
                documentName = "手工入库";
            } else {
                documentName = "手工出库";
            }
        }
        //先去获取这个sku和仓库的数据是否在这条数据的日期之前有出现过，有就查询下来用他的库存单价和剩余库存做后续计算，没有则去初期库存表中查找
        BigDecimal inventoryUnitPrice;
        BigDecimal remainingInventorySum;
        CaravanStorageLogData previousStorageLogData = storageLogDataMapper.getPreviousDataBySkuAndWarehouse(sku, warehouse, timeCreate);
        if (previousStorageLogData == null) {
            LocalDateTime createTime = LocalDateTime.parse(timeCreate, df);
            String time = LocalDateTime.of(createTime.getYear(), createTime.getMonthValue(), 1, 0, 0, 0).format(df);
            FinanceOpeningInventory financeOpeningInventory = financeOpeningInventoryMapper.getDataBySkuAndWarehouseAndTime(sku, warehouse, time);
            inventoryUnitPrice = financeOpeningInventory.getInventoryUnitPrice();
            remainingInventorySum = financeOpeningInventory.getRemainingInventorySum();
        } else {
            inventoryUnitPrice = previousStorageLogData.getInventoryUnitPrice();
            remainingInventorySum = previousStorageLogData.getRemainingInventorySum();
        }
        int documentType = financeCaravanDebitCreditRelationList.get(0).getDocumentType();
        try {
            switch (documentType) {
                case 14:  //"采购入库"
                    //获得关联采购单对应的信息
                    CaravanPurchaseDataItem caravanPurchaseDataItem = purchaseDataMapper.getPurchaseDataItemByPurchaseIdAndSku(documentNum, sku);
                    BigDecimal sellPrice = caravanPurchaseDataItem.getSellPrice();
                    //计算出各个借贷科目对应的值
                    //库存商品
                    BigDecimal goodsInStock0 = BigDecimal.valueOf(financeCaravanDebitCreditRelationList.get(0).getSkuNum()).multiply(sellPrice);
                    //在途物资-采购
                    BigDecimal materialsInTransitByPurchase1 = BigDecimal.valueOf(financeCaravanDebitCreditRelationList.get(1).getSkuNum()).multiply(sellPrice);
                    // 下面这组借贷关系需要去借贷关系表中找账单类型为付款单，关联单号是对应的采购单号，sku是这个sku，借贷关系是借，科目名是“预付款单-采购”的借贷关系金额，
                    // 以此来确定贷方怎么计算金额
                    BigDecimal amountSum = financeCaravanDebitCreditRelationMapper.getAmountByRecordTypeRelatedRecordSkuLoanTypeSubjectId(2, documentNum, sku, 1, 112301, timeCreate);
                    //应付账款-采购暂估
                    BigDecimal accountspayableByPurchaseEstimation2 = BigDecimal.valueOf(financeCaravanDebitCreditRelationList.get(2).getSkuNum()).multiply(sellPrice);
                    //应付账款-采购
                    BigDecimal accountspayableByPurchase3;
                    //预付账款-采购
                    BigDecimal prepaymentByPurchase4;
                    if (amountSum.compareTo(BigDecimal.ZERO) > 0) {
                        prepaymentByPurchase4 = amountSum;
                        accountspayableByPurchase3 = accountspayableByPurchaseEstimation2.subtract(prepaymentByPurchase4);
                    } else {
                        accountspayableByPurchase3 = accountspayableByPurchaseEstimation2;
                        prepaymentByPurchase4 = BigDecimal.ZERO;
                    }
                    loadStorageLogDataDebitCreditRelationValue(financeCaravanDebitCreditRelationList.get(0), goodsInStock0);
                    loadStorageLogDataDebitCreditRelationValue(financeCaravanDebitCreditRelationList.get(1), materialsInTransitByPurchase1);
                    loadStorageLogDataDebitCreditRelationValue(financeCaravanDebitCreditRelationList.get(2), accountspayableByPurchaseEstimation2);
                    loadStorageLogDataDebitCreditRelationValue(financeCaravanDebitCreditRelationList.get(3), accountspayableByPurchase3);
                    loadStorageLogDataDebitCreditRelationValue(financeCaravanDebitCreditRelationList.get(4), prepaymentByPurchase4);
                    //这里需要去查到具体账单种类、关联的采购单号和sku一样的之前的出入库数据，把数量都加起来看看是不是实际入库的比采购单的多，如果是，则再加一组借贷关系
                    Integer quantitySum = storageLogDataMapper.getQuantitySumByDocumentTypeSkuWarehouseDocumentNumTime(documentName, sku, warehouse, documentNum, timeCreate);
                    if (caravanPurchaseDataItem != null && quantitySum > caravanPurchaseDataItem.getPurchaseNum()) {
                        //在途物资-采购
                        BigDecimal materialsInTransitByPurchase5 = BigDecimal.valueOf(financeCaravanDebitCreditRelationList.get(5).getSkuNum()).multiply(sellPrice);
                        //应付账款-采购暂估
                        BigDecimal accountspayableByPurchaseEstimation6 = BigDecimal.valueOf(financeCaravanDebitCreditRelationList.get(6).getSkuNum()).multiply(sellPrice);
                        loadStorageLogDataDebitCreditRelationValue(financeCaravanDebitCreditRelationList.get(5), materialsInTransitByPurchase5);
                        loadStorageLogDataDebitCreditRelationValue(financeCaravanDebitCreditRelationList.get(6), accountspayableByPurchaseEstimation6);
                    }
                    //计算出当前这条数据的剩余库存和库存单价
                    remainingInventorySum = remainingInventorySum.add(BigDecimal.valueOf(quantity).multiply(sellPrice));
                    inventoryUnitPrice = remainingInventorySum.divide(BigDecimal.valueOf(storageLogData.getQuantityAfter()), 4, BigDecimal.ROUND_HALF_UP);
                    storageLogData.setHandleStatus(2);
                    storageLogData.setRemainingInventorySum(remainingInventorySum);
                    storageLogData.setInventoryUnitPrice(inventoryUnitPrice);
                    log.info("采购入库借贷关系金额生成成功");
                    break;
                case 15://采购单退货出库,这个借贷的数据需要反冲，所以要加上符号计算
                    //获得关联采购单对应的信息
                    caravanPurchaseDataItem = purchaseDataMapper.getPurchaseDataItemByPurchaseIdAndSku(documentNum, sku);
                    sellPrice = caravanPurchaseDataItem.getSellPrice();
                    //在途物资-采购
                    BigDecimal materialsInTransitByPurchase0 = BigDecimal.valueOf(financeCaravanDebitCreditRelationList.get(0).getSkuNum()).multiply(sellPrice);
                    //库存商品
                    BigDecimal goodsInStock1 = BigDecimal.valueOf(financeCaravanDebitCreditRelationList.get(1).getSkuNum()).multiply(sellPrice);
                    // 下面这组借贷关系需要去借贷关系表中找账单类型为收款单，关联单号是对应的采购单号，sku是这个sku，借贷关系是借，科目名是“预付款单-采购”的借贷关系金额，
                    // 以此来确定贷方怎么计算金额
                    amountSum = financeCaravanDebitCreditRelationMapper.getAmountByRecordTypeRelatedRecordSkuLoanTypeSubjectId(1, documentNum, sku, 1, 112301, timeCreate);
                    //应付账款-采购暂估
                    accountspayableByPurchaseEstimation2 = BigDecimal.valueOf(financeCaravanDebitCreditRelationList.get(2).getSkuNum()).multiply(sellPrice);
                    if (amountSum.compareTo(BigDecimal.ZERO) > 0) {
                        //预付账款-采购
                        prepaymentByPurchase4 = BigDecimal.ZERO.subtract(amountSum);
                        //应付账款-采购
                        accountspayableByPurchase3 = accountspayableByPurchaseEstimation2.subtract(prepaymentByPurchase4);
                    } else {
                        accountspayableByPurchase3 = accountspayableByPurchaseEstimation2;
                        prepaymentByPurchase4 = BigDecimal.ZERO;
                    }
                    loadStorageLogDataDebitCreditRelationValue(financeCaravanDebitCreditRelationList.get(0), materialsInTransitByPurchase0);
                    loadStorageLogDataDebitCreditRelationValue(financeCaravanDebitCreditRelationList.get(1), goodsInStock1);
                    loadStorageLogDataDebitCreditRelationValue(financeCaravanDebitCreditRelationList.get(2), accountspayableByPurchaseEstimation2);
                    loadStorageLogDataDebitCreditRelationValue(financeCaravanDebitCreditRelationList.get(3), accountspayableByPurchase3);
                    loadStorageLogDataDebitCreditRelationValue(financeCaravanDebitCreditRelationList.get(4), prepaymentByPurchase4);
                    //计算出当前这条数据的剩余库存和库存单价
                    remainingInventorySum = remainingInventorySum.add(BigDecimal.valueOf(quantity).multiply(sellPrice));
                    inventoryUnitPrice = remainingInventorySum.divide(BigDecimal.valueOf(storageLogData.getQuantityAfter()), 4, BigDecimal.ROUND_HALF_UP);
                    storageLogData.setHandleStatus(2);
                    storageLogData.setRemainingInventorySum(remainingInventorySum);
                    storageLogData.setInventoryUnitPrice(inventoryUnitPrice);
                    log.info("采购单退货出库借贷关系金额生成成功");
                    break;
                case 6://加工原材料出库
                    //库存商品
                    goodsInStock0 = BigDecimal.valueOf(financeCaravanDebitCreditRelationList.get(0).getSkuNum()).multiply(inventoryUnitPrice);
                    //原材料
                    BigDecimal rawMaterial1 = BigDecimal.valueOf(financeCaravanDebitCreditRelationList.get(1).getSkuNum()).multiply(inventoryUnitPrice);
                    loadStorageLogDataDebitCreditRelationValue(financeCaravanDebitCreditRelationList.get(0), goodsInStock0);
                    loadStorageLogDataDebitCreditRelationValue(financeCaravanDebitCreditRelationList.get(1), rawMaterial1);
                    //计算出当前这条数据的剩余库存和库存单价
                    remainingInventorySum = remainingInventorySum.add(BigDecimal.valueOf(quantity).multiply(inventoryUnitPrice));
                    inventoryUnitPrice = remainingInventorySum.divide(BigDecimal.valueOf(storageLogData.getQuantityAfter()), 4, BigDecimal.ROUND_HALF_UP);
                    storageLogData.setHandleStatus(2);
                    storageLogData.setRemainingInventorySum(remainingInventorySum);
                    storageLogData.setInventoryUnitPrice(inventoryUnitPrice);
                    log.info("加工原材料出库借贷关系金额生成成功");
                    break;
                case 7://加工商品入库
                    //需要通过加工单数据去求得加工后商品的价格
                    List<FinanceCaravanProcessingOrderItem> processingOrderItemList = caravanProcessingOrderItemMapper.getDataByProcessingDocumentNoWarehouseToSku(documentNum, warehouse, sku);
                    BigDecimal amount = BigDecimal.ZERO;
                    for (FinanceCaravanProcessingOrderItem processingOrderItem : processingOrderItemList) {
                        String fromSku = processingOrderItem.getFromSku();
                        Integer fromSkuNum = processingOrderItem.getFromSkuNum();
                        CaravanStorageLogData fromSkuStorageLogData = storageLogDataMapper.getDataByDocumentTypeSkuWarehouseDocumentNumTime(documentName, fromSku, documentNum, timeCreate);
                        amount = amount.add(BigDecimal.valueOf(fromSkuNum).multiply(fromSkuStorageLogData.getInventoryUnitPrice()));
                    }
                    loadStorageLogDataDebitCreditRelationValue(financeCaravanDebitCreditRelationList.get(0), amount);
                    loadStorageLogDataDebitCreditRelationValue(financeCaravanDebitCreditRelationList.get(1), amount);
                    //计算出当前这条数据的剩余库存和库存单价
                    remainingInventorySum = remainingInventorySum.add(amount);
                    inventoryUnitPrice = remainingInventorySum.divide(BigDecimal.valueOf(storageLogData.getQuantityAfter()), 4, BigDecimal.ROUND_HALF_UP);
                    storageLogData.setHandleStatus(2);
                    storageLogData.setRemainingInventorySum(remainingInventorySum);
                    storageLogData.setInventoryUnitPrice(inventoryUnitPrice);
                    log.info("加工商品入库借贷关系金额生成成功");
                    break;
                case 4://分仓调拨入库
                    //在途物资-调拨在途
                    BigDecimal inTransitMaterialsByTransferInTransit0 = BigDecimal.valueOf(financeCaravanDebitCreditRelationList.get(0).getSkuNum()).multiply(inventoryUnitPrice);
                    //库存商品
                    goodsInStock1 = BigDecimal.valueOf(financeCaravanDebitCreditRelationList.get(1).getSkuNum()).multiply(inventoryUnitPrice);
                    loadStorageLogDataDebitCreditRelationValue(financeCaravanDebitCreditRelationList.get(0), inTransitMaterialsByTransferInTransit0);
                    loadStorageLogDataDebitCreditRelationValue(financeCaravanDebitCreditRelationList.get(1), goodsInStock1);
                    //计算出当前这条数据的剩余库存和库存单价
                    remainingInventorySum = remainingInventorySum.add(BigDecimal.valueOf(quantity).multiply(inventoryUnitPrice));
                    inventoryUnitPrice = remainingInventorySum.divide(BigDecimal.valueOf(storageLogData.getQuantityAfter()), 4, BigDecimal.ROUND_HALF_UP);
                    storageLogData.setHandleStatus(2);
                    storageLogData.setRemainingInventorySum(remainingInventorySum);
                    storageLogData.setInventoryUnitPrice(inventoryUnitPrice);
                    log.info("分仓调拨入库借贷关系金额生成成功");
                    break;
                case 5://分仓调拨出库
                    //库存商品
                    goodsInStock0 = BigDecimal.valueOf(financeCaravanDebitCreditRelationList.get(0).getSkuNum()).multiply(inventoryUnitPrice);
                    //在途物资-调拨在途
                    BigDecimal inTransitMaterialsByTransferInTransit1 = BigDecimal.valueOf(financeCaravanDebitCreditRelationList.get(1).getSkuNum()).multiply(inventoryUnitPrice);
                    loadStorageLogDataDebitCreditRelationValue(financeCaravanDebitCreditRelationList.get(0), goodsInStock0);
                    loadStorageLogDataDebitCreditRelationValue(financeCaravanDebitCreditRelationList.get(1), inTransitMaterialsByTransferInTransit1);
                    //计算出当前这条数据的剩余库存和库存单价
                    remainingInventorySum = remainingInventorySum.add(BigDecimal.valueOf(quantity).multiply(inventoryUnitPrice));
                    inventoryUnitPrice = remainingInventorySum.divide(BigDecimal.valueOf(storageLogData.getQuantityAfter()), 4, BigDecimal.ROUND_HALF_UP);
                    storageLogData.setHandleStatus(2);
                    storageLogData.setRemainingInventorySum(remainingInventorySum);
                    storageLogData.setInventoryUnitPrice(inventoryUnitPrice);
                    log.info("分仓调拨出库借贷关系金额生成成功");
                    break;
                case 3://分仓调拨作废入库
                    //库存商品
                    goodsInStock0 = BigDecimal.valueOf(financeCaravanDebitCreditRelationList.get(0).getSkuNum()).multiply(inventoryUnitPrice);
                    //在途物资-调拨在途
                    inTransitMaterialsByTransferInTransit1 = BigDecimal.valueOf(financeCaravanDebitCreditRelationList.get(1).getSkuNum()).multiply(inventoryUnitPrice);
                    loadStorageLogDataDebitCreditRelationValue(financeCaravanDebitCreditRelationList.get(0), goodsInStock0);
                    loadStorageLogDataDebitCreditRelationValue(financeCaravanDebitCreditRelationList.get(1), inTransitMaterialsByTransferInTransit1);
                    //计算出当前这条数据的剩余库存和库存单价
                    remainingInventorySum = remainingInventorySum.add(BigDecimal.valueOf(quantity).multiply(inventoryUnitPrice));
                    inventoryUnitPrice = remainingInventorySum.divide(BigDecimal.valueOf(storageLogData.getQuantityAfter()), 4, BigDecimal.ROUND_HALF_UP);
                    storageLogData.setHandleStatus(2);
                    storageLogData.setRemainingInventorySum(remainingInventorySum);
                    storageLogData.setInventoryUnitPrice(inventoryUnitPrice);
                    log.info("分仓调拨作废入库借贷关系金额生成成功");
                    break;
                case 13://退货单验货入库
                    //需要去查找到有一样关联批次号且离自身数据时间最近的那条数据，有就用这条数据的库存单价；没有就用自己当前的
                    List<FinanceCaravanDebitCreditRelation> orderIssueDebitCreditRelationList = financeCaravanDebitCreditRelationMapper.getDataByDocumentTypeSkuWarehouseRelatedRecordNoTime(documentType, sku, warehouse, documentNum, timeCreate);
                    if (orderIssueDebitCreditRelationList == null && orderIssueDebitCreditRelationList.size() == 0) {
                        //库存商品
                        goodsInStock0 = BigDecimal.valueOf(financeCaravanDebitCreditRelationList.get(0).getSkuNum()).multiply(inventoryUnitPrice);
                        //主营业务成本-历史退货
                        BigDecimal mainBusinessCostByHistoricalReturn1 = BigDecimal.valueOf(financeCaravanDebitCreditRelationList.get(1).getSkuNum()).multiply(inventoryUnitPrice);
                        loadStorageLogDataDebitCreditRelationValue(financeCaravanDebitCreditRelationList.get(0), goodsInStock0);
                        loadStorageLogDataDebitCreditRelationValue(financeCaravanDebitCreditRelationList.get(1), mainBusinessCostByHistoricalReturn1);
                    }
                    //计算出当前这条数据的剩余库存和库存单价
                    remainingInventorySum = remainingInventorySum.subtract(financeCaravanDebitCreditRelationList.get(0).getAmount());
                    inventoryUnitPrice = remainingInventorySum.divide(BigDecimal.valueOf(storageLogData.getQuantityAfter()), 4, BigDecimal.ROUND_HALF_UP);
                    storageLogData.setHandleStatus(2);
                    storageLogData.setRemainingInventorySum(remainingInventorySum);
                    storageLogData.setInventoryUnitPrice(inventoryUnitPrice);
                    log.info("退货单验货入库借贷关系金额生成成功");
                    break;
                case 10://手工出库
                    //手动确认
                    BigDecimal manualConfirmation0 = BigDecimal.valueOf(financeCaravanDebitCreditRelationList.get(0).getSkuNum()).multiply(inventoryUnitPrice);
                    //库存商品
                    goodsInStock1 = BigDecimal.valueOf(financeCaravanDebitCreditRelationList.get(1).getSkuNum()).multiply(inventoryUnitPrice);
                    loadStorageLogDataDebitCreditRelationValue(financeCaravanDebitCreditRelationList.get(0), manualConfirmation0);
                    loadStorageLogDataDebitCreditRelationValue(financeCaravanDebitCreditRelationList.get(1), goodsInStock1);
                    //计算出当前这条数据的剩余库存和库存单价
                    remainingInventorySum = remainingInventorySum.add(BigDecimal.valueOf(quantity).multiply(inventoryUnitPrice));
                    inventoryUnitPrice = remainingInventorySum.divide(BigDecimal.valueOf(storageLogData.getQuantityAfter()), 4, BigDecimal.ROUND_HALF_UP);
                    storageLogData.setHandleStatus(2);
                    storageLogData.setRemainingInventorySum(remainingInventorySum);
                    storageLogData.setInventoryUnitPrice(inventoryUnitPrice);
                    log.info("手工出库借贷关系金额生成成功");
                    break;
                case 9://手工入库
                    //库存商品
                    goodsInStock0 = BigDecimal.valueOf(financeCaravanDebitCreditRelationList.get(0).getSkuNum()).multiply(inventoryUnitPrice);
                    //手动确认
                    BigDecimal manualConfirmation1 = BigDecimal.valueOf(financeCaravanDebitCreditRelationList.get(1).getSkuNum()).multiply(inventoryUnitPrice);
                    loadStorageLogDataDebitCreditRelationValue(financeCaravanDebitCreditRelationList.get(0), goodsInStock0);
                    loadStorageLogDataDebitCreditRelationValue(financeCaravanDebitCreditRelationList.get(1), manualConfirmation1);
                    //计算出当前这条数据的剩余库存和库存单价
                    remainingInventorySum = remainingInventorySum.add(BigDecimal.valueOf(quantity).multiply(inventoryUnitPrice));
                    inventoryUnitPrice = remainingInventorySum.divide(BigDecimal.valueOf(storageLogData.getQuantityAfter()), 4, BigDecimal.ROUND_HALF_UP);
                    storageLogData.setHandleStatus(2);
                    storageLogData.setRemainingInventorySum(remainingInventorySum);
                    storageLogData.setInventoryUnitPrice(inventoryUnitPrice);
                    log.info("手工入库借贷关系金额生成成功");
                    break;
                case 2://修改库存
                    if (quantity >= 0) {
                        //库存商品
                        goodsInStock0 = BigDecimal.valueOf(financeCaravanDebitCreditRelationList.get(0).getSkuNum()).multiply(inventoryUnitPrice);
                        //营业外收入-盘盈
                        BigDecimal nonbusinessIncomeByInventoryProfit1 = BigDecimal.valueOf(financeCaravanDebitCreditRelationList.get(1).getSkuNum()).multiply(inventoryUnitPrice);
                        loadStorageLogDataDebitCreditRelationValue(financeCaravanDebitCreditRelationList.get(0), goodsInStock0);
                        loadStorageLogDataDebitCreditRelationValue(financeCaravanDebitCreditRelationList.get(1), nonbusinessIncomeByInventoryProfit1);
                    } else {
                        //库存商品
                        goodsInStock0 = BigDecimal.valueOf(financeCaravanDebitCreditRelationList.get(0).getSkuNum()).multiply(inventoryUnitPrice);
                        //营业外收入-盘亏
                        BigDecimal nonbusinessIncomeByInventoryLoss1 = BigDecimal.valueOf(financeCaravanDebitCreditRelationList.get(1).getSkuNum()).multiply(inventoryUnitPrice);
                        loadStorageLogDataDebitCreditRelationValue(financeCaravanDebitCreditRelationList.get(0), goodsInStock0);
                        loadStorageLogDataDebitCreditRelationValue(financeCaravanDebitCreditRelationList.get(1), nonbusinessIncomeByInventoryLoss1);
                    }
                    //计算出当前这条数据的剩余库存和库存单价
                    remainingInventorySum = remainingInventorySum.add(BigDecimal.valueOf(quantity).multiply(inventoryUnitPrice));
                    inventoryUnitPrice = remainingInventorySum.divide(BigDecimal.valueOf(storageLogData.getQuantityAfter()), 4, BigDecimal.ROUND_HALF_UP);
                    storageLogData.setHandleStatus(2);
                    storageLogData.setRemainingInventorySum(remainingInventorySum);
                    storageLogData.setInventoryUnitPrice(inventoryUnitPrice);
                    log.info("修改库存借贷关系金额生成成功");
                    break;
                case 12://订单出库
                    //这里需要去查到关联的订单数据，看订单中到达的店铺名是否和这个sku匹配的店铺名相同
                    CaravanOrderData caravanOrderData = orderDataMapper.getShopNameByPlatformOrderId(documentNum);
                    String targetShopName = caravanOrderData.getShopName();
                    String platformId = caravanOrderData.getPlatformId();
                    FinanceShopSku financeShopSku = shopSkuMapper.getInfoBySku(sku);
                    String sellerId = financeShopSku.getSellerId();
                    String area = financeShopSku.getArea();
                    FinanceCaravanShopNameRelation financeCaravanShopNameRelation = financeCaravanShopNameRelationMapper.getInfoByCaravanShopNameAndPlatform(targetShopName, platformId);
                    String targetSellerId = "";
                    String targetArea = "";
//                String shopName = "";
                    if (financeCaravanShopNameRelation != null) {
                        targetSellerId = financeCaravanShopNameRelation.getSelllerId();
                        targetArea = financeCaravanShopNameRelation.getArea();
//                    shopName = financeCaravanShopNameRelation.getShopName();
                    }
                    if (sellerId.equals(targetSellerId) && area.equals(targetArea)) {
                        //主营业务成本-自配送
                        BigDecimal mainBusinessCostBySelfDelivery0 = BigDecimal.valueOf(financeCaravanDebitCreditRelationList.get(0).getSkuNum()).multiply(inventoryUnitPrice);
                        //库存商品
                        goodsInStock1 = BigDecimal.valueOf(financeCaravanDebitCreditRelationList.get(1).getSkuNum()).multiply(inventoryUnitPrice);
                        loadStorageLogDataDebitCreditRelationValue(financeCaravanDebitCreditRelationList.get(0), mainBusinessCostBySelfDelivery0);
                        loadStorageLogDataDebitCreditRelationValue(financeCaravanDebitCreditRelationList.get(1), goodsInStock1);
                    } else {
                        //库存商品
                        goodsInStock1 = BigDecimal.valueOf(financeCaravanDebitCreditRelationList.get(1).getSkuNum()).multiply(inventoryUnitPrice);
                        //代购利润
                        BigDecimal agentPurchaseProfit2 = BigDecimal.valueOf(financeCaravanDebitCreditRelationList.get(1).getSkuNum()).multiply(BigDecimal.ONE);
                        //主营业务成本-自配送
                        BigDecimal mainBusinessCostBySelfDelivery0 = goodsInStock1.add(agentPurchaseProfit2);
                        loadStorageLogDataDebitCreditRelationValue(financeCaravanDebitCreditRelationList.get(0), mainBusinessCostBySelfDelivery0);
                        loadStorageLogDataDebitCreditRelationValue(financeCaravanDebitCreditRelationList.get(1), goodsInStock1);
                        loadStorageLogDataDebitCreditRelationValue(financeCaravanDebitCreditRelationList.get(2), agentPurchaseProfit2);
                    }
                    //计算出当前这条数据的剩余库存和库存单价
                    remainingInventorySum = remainingInventorySum.add(BigDecimal.valueOf(quantity).multiply(inventoryUnitPrice));
                    inventoryUnitPrice = remainingInventorySum.divide(BigDecimal.valueOf(storageLogData.getQuantityAfter()), 4, BigDecimal.ROUND_HALF_UP);
                    storageLogData.setHandleStatus(2);
                    storageLogData.setRemainingInventorySum(remainingInventorySum);
                    storageLogData.setInventoryUnitPrice(inventoryUnitPrice);
                    log.info("订单出库借贷关系金额生成成功");
                    break;
                case 1://FBA发货出库
                    //这里需要去查到关联的亚马逊发货批次数据，看到达的店铺名是否和这个sku匹配的店铺名相同
                    WlCreateAPlan wlCreateAPlan = wlCreateAPlanMapper.getShopDataByShipmentId(documentNum);
                    financeShopSku = shopSkuMapper.getInfoBySku(sku);
                    sellerId = financeShopSku.getSellerId();
                    area = financeShopSku.getArea();
                    targetSellerId = "";
                    targetArea = "";
                    if (wlCreateAPlan != null) {
                        targetSellerId = wlCreateAPlan.getSellerId();
                        targetArea = wlCreateAPlan.getCountryName();
                    }
                    if (sellerId.equals(targetSellerId) && area.equals(targetArea)) {
                        //在途物资-（子科目名，已经在生成科目名时确定）
                        BigDecimal materialsInTransit0 = BigDecimal.valueOf(financeCaravanDebitCreditRelationList.get(0).getSkuNum()).multiply(inventoryUnitPrice);
                        //库存商品
                        goodsInStock1 = BigDecimal.valueOf(financeCaravanDebitCreditRelationList.get(1).getSkuNum()).multiply(inventoryUnitPrice);
                        loadStorageLogDataDebitCreditRelationValue(financeCaravanDebitCreditRelationList.get(0), materialsInTransit0);
                        loadStorageLogDataDebitCreditRelationValue(financeCaravanDebitCreditRelationList.get(1), goodsInStock1);
                    } else {
                        //库存商品
                        goodsInStock1 = BigDecimal.valueOf(financeCaravanDebitCreditRelationList.get(1).getSkuNum()).multiply(inventoryUnitPrice);
                        //代购利润
                        BigDecimal agentPurchaseProfit2 = BigDecimal.valueOf(financeCaravanDebitCreditRelationList.get(1).getSkuNum()).multiply(BigDecimal.ONE);
                        //在途物资-（子科目名，已经在生成科目名时确定）
                        BigDecimal materialsInTransit0 = goodsInStock1.add(agentPurchaseProfit2);
                        loadStorageLogDataDebitCreditRelationValue(financeCaravanDebitCreditRelationList.get(0), materialsInTransit0);
                        loadStorageLogDataDebitCreditRelationValue(financeCaravanDebitCreditRelationList.get(1), goodsInStock1);
                        loadStorageLogDataDebitCreditRelationValue(financeCaravanDebitCreditRelationList.get(2), agentPurchaseProfit2);
                    }
                    log.info("FBA发货出库借贷关系金额生成成功");
                    break;
                case 8://库存盘点
                case 11://海外仓出入库
                case 16://excel导入
                    loadStorageLogDataDebitCreditRelationValue(financeCaravanDebitCreditRelationList.get(0), BigDecimal.ZERO);
                    loadStorageLogDataDebitCreditRelationValue(financeCaravanDebitCreditRelationList.get(1), BigDecimal.ZERO);
                    log.info("全手动处理借贷关系金额生成成功");
                    break;
                default:
                    break;
            }
        } catch (Exception e) {
            log.error("具体状态为：" + documentType + "的借贷关系生成失败");
            e.printStackTrace();
        }
    }

    /**
     * 通用的生成出入库流水的借贷关系的方法
     */
    private FinanceCaravanDebitCreditRelation loadStorageLogDataDebitCreditRelation(CaravanStorageLogData storageLogData, int documentType, int subjectId, int loanType) {
        DateTimeFormatter df = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
        String nowTime = LocalDateTime.now().format(df);
        String sku = storageLogData.getStockSku();
        FinanceCaravanDebitCreditRelation debitCreditRelation = new FinanceCaravanDebitCreditRelation();
        debitCreditRelation.setRecordNo(storageLogData.getLogId());
        debitCreditRelation.setRecordType(5);
        //获得当前这条数据的sku对应的店铺信息
        FinanceShopSku financeShopSku = shopSkuMapper.getInfoBySku(sku);
        debitCreditRelation.setSellerId(financeShopSku.getSellerId());
        debitCreditRelation.setShopName(financeShopSku.getShopName());
        debitCreditRelation.setArea(financeShopSku.getArea());
        debitCreditRelation.setSku(sku);
        debitCreditRelation.setSkuNum(storageLogData.getQuantity());
        debitCreditRelation.setWarehouse(storageLogData.getWearhouseName());
        debitCreditRelation.setRelatedRecordNo(storageLogData.getDocumentNum());
        debitCreditRelation.setDocumentType(documentType);
        debitCreditRelation.setLoanType(loanType);
        //这里需要根据subjectID查找到子父科目名
        FinanceSubject financeSubject = subjectMapper.getInfoBysubjectId(subjectId);
        debitCreditRelation.setSubjectId(subjectId);
        debitCreditRelation.setPSubjectName(financeSubject.getPSubject());
        debitCreditRelation.setCSubjectName(financeSubject.getSubject());
        debitCreditRelation.setDataTime(storageLogData.getTimeCreate());
        debitCreditRelation.setRelatedPerson(storageLogData.getOperName());
        debitCreditRelation.setCreator("系统生成");
        debitCreditRelation.setCreateTime(nowTime);
        debitCreditRelation.setStatus(1);
        return debitCreditRelation;
    }

    /**
     * 通用的生成出入库流水的借贷关系值的方法
     */
    private FinanceCaravanDebitCreditRelation loadStorageLogDataDebitCreditRelationValue(FinanceCaravanDebitCreditRelation debitCreditRelation, BigDecimal amount) {
        debitCreditRelation.setAmount(amount);
        return debitCreditRelation;
    }
}
